<template>
    <div>
      <table-expand
        ref="table"
        :columns="columns"
        :data="data"
        :loading="loading"
        :config="config"
        :pageSizeOpts="pageSizeOpts"
        :pageNavigation="pageNavigation"
        :queryFormItem="queryFormItem"
        :collapseFormItem="collapseFormItem"
        :additionalAction="additionalAction"
        :action="action"
        @loadPage="loadPage"
        @handleCreate="handleCreate"
        @handleDetail="handleDetail"
        @handleEdit="handleEdit"
        @handleDelete="handleDelete"
        @handleDeleteMultiple="handleDeleteMultiple"
        @handleSearch="handleSearch"
        @handleReset="handleReset"
      ></table-expand>
      <read-modal
        v-bind="readPanel"
        :modelType="modelType"
        :columns="columns"
        :rules="rules"
      ></read-modal>
      <!-- 抄表 -->
      <single-modal v-bind="resultPanel" :title="'new'" @submit="handleResultSubmit">
        <formRule ref="_formRule" :data="resultPanel.data" :rules="rules" :cols="resultPanel.cols" :columns="resultColumns"></formRule>
      </single-modal>
    </div>
</template>

  <script>
    import tableExpand from '@/pages/components/tableExpand.vue';
    import readModal from '@/pages/components/readModal.vue';
    import { typeClassName } from '@/pages/config/classNameMap';
    import mixin from '@/pages/components/mixins/mixin'
    import singleModal from '@/pages/components/singleModal.vue';
    import formRule from '@/pages/components/formRule';
    import columnsMixin from './config'
    export default {
        components: { readModal, tableExpand, singleModal, formRule },
        name: 'meterRegister',
        mixins: [mixin, columnsMixin],
        data () {
            return {
                classObject: null, // 单个实例
                collectionObject: null, // 所有实例
                className: typeClassName['equipment'].className,
                type: typeClassName['equipment'].type,
                modelType: typeClassName['equipment'].modelType,
                isSingleCheck: false,
                readPanel: {
                    width: 1024,
                    zIndex: 100,
                    titleStyle: {},
                    drawer: {
                        show: false
                    },
                    cols: 2,
                    data: {}
                },
                resultPanel: {
                    width: 1024,
                    zIndex: 100,
                    titleStyle: {},
                    drawer: {
                        show: false
                    },
                    cols: 2,
                    data: {}
                },
                rules: {
                    sf_code: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ],
                    sf_name: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ]
                },
                loading: false,
                config: {
                    search: true,
                    addButton: false,
                    batchDeleteButton: false,
                    tableSizeIcon: true,
                    fullscreenIcon: true,
                    refreshIcon: false,
                    columnSettingIcon: true,
                    action: true,
                    additionalAction: true
                },
                action: {
                    edit: false,
                    delete: false,
                    detail: true
                },
                pageSizeOpts: [10, 30, 50, 100],
                pageNavigation: { total: 0, pageSize: 10, pageNumber: 1 },
                equipTypeClassObject: null,
                equipTypeOptions: [], // 类别名称选项
                // 进入页面需要的筛选条件：所属分类，'0'设备和'1'仪表
                selfFilter: { 'sf_category': '1', 'sf_status': '启用' },
            };
        },
        computed: {
            additionalAction () {
                let additionalAction = {
                    key: 'additionalAction',
                    title: {
                        'zh-CN': '附加操作',
                        'en-US': 'additional action'
                    }[this.G_Locale],
                    slot: 'additionalAction',
                    width: 200,
                    fixed: 'right',
                    align: 'center',
                    show: true,
                    buttons: [
                        {
                            title: {
                                'zh-CN': '抄表',
                                'en-US': 'register'
                            }[this.G_Locale],
                            event: (row, index) => this.register(row, index)
                        }
                    ]
                };
                this.G_IsMobile && (additionalAction.width = 90)
                return additionalAction;
            },
            data () {
                if (!!this.collectionObject) {
                    return this.collectionObject.records.map(d => {
                        return {
                            ...d,
                            ...d.values
                        }
                    })
                } else {
                    return [];
                }
            }
        },
        beforeMount () {
            this.collectionObject = this.$modelObject.create(`${this.modelType}Collection`);
            this.classObject = this.$modelObject.create(this.modelType);
            this.equipTypeClassObject = this.$modelObject.create(typeClassName['equipType'].modelType);
            this.setOptionsByHier('equipType', this.equipTypeClassObject, 'equipTypeOptions', {
                label: 'sf_name', value: 'sf_code', category: 'category'
            })
            this.loadPage(this.pageNavigation, null);
        },
        mounted () {},
        methods: {
          // 抄表
            async register (row, index) {
                this.getRecord({ type: row.type, className: row.className, id: row.id }).then(flag => {
                    if (flag) {
                        this.$set(this.resultPanel.drawer, 'show', true)
                        let {sf_code,sf_name} = {...this.classObject.record.values}
                        this.$set(this.resultPanel, 'data', {sf_code,sf_name})
                    }
                })
            },
            // 抄表执行
            handleResultSubmit () {
                this.$refs._formRule.$refs.form.validate(valid => {
                    if (valid) {
                        let dataParams = {
                            ...this.classObject.record,
                            expand: this.resultPanel.data
                        }
                        this.$api.Custom.meterRecord(dataParams).then(res => {
                            this.$set(this.resultPanel.drawer, 'show', false);
                            this.classObject.setRecord(res);
                        })
                    }
                });
            }
        }
    };
  </script>
  <style scoped lang="scss"></style>
